﻿using CodeMan.Domain.ClassGens;
using CodeMan.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using CodeMan.Domain;
using BootstrapBlazor.Components;

namespace CodeMan.EntityFrameworkCore.ClassGens
{
    public class ClassAttributeRepository : EfCoreRepository<CodeManDbContext, ClassAttribute>,
        IClassAttributeRepository

    {
        private DbContext _db;
        private ClassAttribute? Model { get; set; }

        public ClassAttributeRepository(IDbContextProvider<CodeManDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        private async Task<bool> GetDbbContext()
        {
            _db = await GetDbContextAsync();
            return true;
        }

        public Task<bool> AddAsync(ClassAttribute model)
        {
            Model = model; 
            return Task.FromResult(true);
        }

        public async Task CancelAsync()
        {
            await GetDbbContext();

            if (Model != null)
            {
                if (!Model.AName.isNull()) _db.Entry(Model).State = EntityState.Unchanged;
                else _db.Entry(Model).State = EntityState.Detached;
            }
            await Task.CompletedTask;
        }

        public async Task<bool> DeleteAsync(IEnumerable<ClassAttribute> models)
        {
            await GetDbbContext();

            _db.RemoveRange(models);
            await _db.SaveChangesAsync();

            return true;

        }

        public Task EditAsync(object model)
        {
            Model = model as ClassAttribute;
            return Task.CompletedTask;
        }

        public async Task<bool> SaveAsync(ClassAttribute model, ItemChangedType changedType)
        {
            await GetDbbContext();
            if (Model == null)
            {
                await _db.AddAsync(model);
            }
            else if (Model.ClassGenId == model.ClassGenId && Model.AName == model.AName)
            {
                _db.Update(model);
            }
            else
            {
                await _db.AddAsync(model);
            }

            //await _db.SaveChangesAsync();

            return true;
        }

        public async Task<QueryData<ClassAttribute>> QueryAsync(QueryPageOptions qOption)
        {
            await GetDbbContext();

            // 处理过滤与快捷搜索框逻辑
            var query = await _db.Set<ClassAttribute>()
                .Where(qOption.Searchs.GetFilterLambda<ClassAttribute>(FilterLogic.Or), qOption.Searchs.Any())
                .Where(qOption.Filters.GetFilterLambda<ClassAttribute>(), qOption.Filters.Any())
                .Sort(qOption.SortName!, qOption.SortOrder, !string.IsNullOrEmpty(qOption.SortName))
                .Count(out var count)
                .Page((qOption.PageIndex - 1) * qOption.PageItems, qOption.PageItems)
                .ToListAsync();

            // 注意：未处理搜索，此处设置 IsSearched=true 后会导致高级搜索按钮高亮
            var ret = new QueryData<ClassAttribute>()
            {
                TotalCount = count,
                Items = query,
                IsSorted = true,
                IsFiltered = true
            };
            return ret;
        }
        Type GetFilterLambdaType { get; set; } = typeof(ClassAttribute);
        public void SetTypeSearch(Type type)
        {

        }

        public Task<bool> AddDetailAsync(object keyid, ClassAttribute model)
        {
            Model = model;
            Model.ClassGenId = keyid.TryToInt64();
            return Task.FromResult(true);
        }

        public async Task<QueryData<ClassAttribute>> QueryDetailAsync(object keyid, QueryPageOptions qOption)
        {
            await GetDbbContext();

            // 处理过滤与快捷搜索框逻辑
            var query = await _db.Set<ClassAttribute>()
                .Where(a => a.ClassGenId == keyid.TryToInt64())
                .Where(qOption.Searchs.GetFilterLambda<ClassAttribute>(FilterLogic.Or), qOption.Searchs.Any())
                .Where(qOption.Filters.GetFilterLambda<ClassAttribute>(), qOption.Filters.Any())
                .Sort(qOption.SortName!, qOption.SortOrder, !string.IsNullOrEmpty(qOption.SortName))
                .Count(out var count)
                .Page((qOption.PageIndex - 1) * qOption.PageItems, qOption.PageItems)
                .ToListAsync();

            // 注意：未处理搜索，此处设置 IsSearched=true 后会导致高级搜索按钮高亮
            var ret = new QueryData<ClassAttribute>()
            {
                TotalCount = count,
                Items = query,
                IsSorted = true,
                IsFiltered = true
            };
            return ret;
        }
    }
}
